home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / medowl.zip / SOURCE / MEDTVIEW.CPP < prev    next >
C/C++ Source or Header  |  1994-08-11  |  6KB  |  244 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   include\owl\editview.cpp
  4. //   Implements class TEditView
  5. //----------------------------------------------------------------------------
  6. #pragma hdrignore SECTION
  7. #include <owl\owlpch.h>
  8. #if defined(MAGMA)
  9. #include <owl\medtview.h>
  10. #else
  11. #include <owl\editview.h>
  12. #endif
  13. #include <owl\docview.rh>
  14. #include <owl\editview.rh>
  15.  
  16. DIAG_DECLARE_GROUP(OwlDocView);        // General Doc/View diagnostic group
  17.  
  18. #if !defined(SECTION) || SECTION == 1
  19.  
  20. #if defined(MAGMA)
  21. #define MAX_EDIT_BUF (0x7FFFFFFFL)  // can't add new chars after 30,000 bytes
  22. #else
  23. #define MAX_EDIT_BUF (30000)  // can't add new chars after 30,000 bytes
  24. #endif
  25.  
  26. //
  27. //  class TMagmaEditView
  28. //  ----- ---------
  29. //
  30. DEFINE_RESPONSE_TABLE1(TMagmaEditView, TMagmaEditSearch)
  31.   EV_VN_DOCCLOSED,
  32.   EV_VN_ISWINDOW,
  33.   EV_VN_ISDIRTY,
  34.   EV_VN_COMMIT,
  35.   EV_VN_REVERT,
  36.   EV_WM_NCDESTROY,
  37. END_RESPONSE_TABLE;
  38.  
  39. TMagmaEditView::TMagmaEditView(TDocument& doc, TWindow* parent)
  40.          : TMagmaEditSearch(parent, GetNextViewId()), TView(doc), Origin(0)
  41. {
  42.   Attr.AccelTable = IDA_EDITVIEW;
  43.   SetViewMenu(new TMenuDescr(IDM_EDITVIEW,0,2,0,0,0,1));
  44. }
  45.  
  46. void
  47. TMagmaEditView::EvNCDestroy()
  48. {
  49. #if !defined(__WIN32__) && !defined(MAGMA)
  50.   HGLOBAL hdl = (HGLOBAL)::GlobalHandle((UINT)GetWindowWord(GWW_HINSTANCE));
  51. #endif
  52.   TMagmaEditSearch::EvNCDestroy();// call TWindow::EvNCDestroy, this may be deleted
  53. #if !defined(__WIN32__) && !defined(MAGMA)
  54.   if (hdl) {
  55.     ::GlobalUnlock(hdl);
  56.     ::GlobalFree(hdl);
  57.   }
  58. #endif
  59. }
  60.  
  61. TMagmaEditView::~TMagmaEditView()
  62. {
  63. }
  64.  
  65. BOOL
  66. TMagmaEditView::VnDocClosed(int omode)
  67. {
  68.   if (VnIsDirty() || !(omode & ofWrite))  // make sure someone else's write
  69.     return FALSE;
  70.  
  71. #if defined(MAGMA)
  72.   LONG top = GetFirstVisibleLine();
  73. #else
  74.   int top = GetFirstVisibleLine();
  75. #endif
  76.   UINT selbeg;
  77.   UINT selend;
  78.   TMagmaEdit::GetSelection(selbeg, selend);
  79.   TMagmaEdit::Clear();
  80.   LoadData();
  81.   Scroll(0, top);
  82.   TMagmaEdit::SetSelection(selbeg, selend);
  83.  
  84.   return TRUE;
  85. }
  86.  
  87. BOOL
  88. TMagmaEditView::LoadData()
  89. {
  90.   istream* inStream;
  91.   if ((inStream = Doc->InStream(ios::in | ios::binary)) == 0) {
  92.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  93.     return FALSE;
  94.   }
  95.  
  96. #if defined(MAGMAX)
  97.   int status = HandleMessage(ME_OPENFILE, inStream->fd(), NULL);
  98.  
  99. #else
  100.   inStream->seekg(0L, ios::end);
  101.   unsigned long total = inStream->tellg();
  102.   inStream->seekg(0L, ios::beg);
  103.   UINT count = (total > MAX_EDIT_BUF) ? MAX_EDIT_BUF : (UINT)total;
  104.   char far* buf = LockBuffer(count + 1);
  105.   if (!buf) {
  106.     delete inStream;
  107. //    THROW( TXOutOfMemory() );
  108.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  109.     return FALSE;
  110.   }
  111.  
  112.   UINT len;
  113.   inStream->read(buf, len = count);
  114.  
  115.   BOOL status = (inStream->gcount() == len);
  116.   buf[count] = 0;    // 0 terminate buffer
  117.   UnlockBuffer(buf, TRUE);
  118. #endif
  119.  
  120.   delete inStream;   // close file in case process switch
  121.   if (!status)
  122.     Doc->PostError(IDS_READERROR, MB_OK);
  123.  
  124.   return status;
  125. }
  126.  
  127. BOOL
  128. TMagmaEditView::Create()
  129. {
  130.   TRY {
  131.     TMagmaEditSearch::Create();   // throws exception TWindow::TXWindow
  132.   }
  133.   CATCH( (TXOwl& x) {
  134.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  135.     NotOK();
  136.     return TRUE;   // cannot return FALSE - throws another exception
  137.   })
  138.   if (Doc->GetDocPath() == 0) {  
  139.     return TRUE;           // new file, no data to display
  140.   }
  141.   if (!LoadData())
  142.     NotOK();
  143.   return TRUE;
  144. }
  145.  
  146. void
  147. TMagmaEditView::PerformCreate(int menuOrId)
  148. {
  149. #if defined(__WIN32__) || defined(MAGMA)
  150.   HINSTANCE hInst = *GetModule();
  151. #else
  152.   HGLOBAL hdl = ::GlobalAlloc(GHND, 256);  // will grow as needed
  153.   if (!hdl) {
  154. //  Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  155. //  return;
  156.     THROW( TXOutOfMemory() );
  157.   }
  158.   WORD editDS = FP_SEG(::GlobalLock(hdl));
  159.   ::LocalInit(editDS, 0, 0);
  160.   ::GlobalUnlock(hdl);
  161.   HINSTANCE hInst = (HINSTANCE)hdl;
  162. #endif
  163.  
  164. // if (doc readonly || size too large) Attr.Style |= ES_READONLY;
  165.   HWindow = CreateWindowEx(Attr.ExStyle,
  166.                            GetClassName(),
  167.                            Title,
  168.                            Attr.Style,
  169.                            Attr.X, Attr.Y, Attr.W, Attr.H,
  170.                            Parent ? Parent->HWindow : 0,
  171.                            (HMENU)menuOrId,
  172.                            hInst,
  173.                            Attr.Param);
  174. }
  175.  
  176. BOOL
  177. TMagmaEditView::VnCommit(BOOL force)
  178. {
  179.   if (!force && !(VnIsDirty()))
  180.     return TRUE;
  181.  
  182.   ostream* outStream;
  183.   if ((outStream = Doc->OutStream(ios::out | ios::binary)) == 0) {
  184.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  185.     return FALSE;
  186.   }
  187.  
  188. #if defined(MAGMAX)
  189.   int status = HandleMessage(ME_WRITEFILE, outStream->fd, NULL);
  190.  
  191. #else
  192.   outStream->seekp(Origin);
  193.  
  194.   BOOL status = FALSE;
  195.   char far* buf = LockBuffer();
  196.   if (buf) {
  197.     UINT count = strlen(buf);
  198.     outStream->write(buf, count);
  199.     status = outStream->good();
  200.     UnlockBuffer(buf);
  201.     ClearModify();   // reset edit control
  202.   }
  203. #endif
  204.  
  205.   delete outStream;
  206.   if (!status)
  207.     Doc->PostError(IDS_WRITEERROR, MB_OK);
  208.  
  209.   return status;
  210. }
  211.  
  212. BOOL
  213. TMagmaEditView::VnRevert(BOOL clear)
  214. {
  215.   TMagmaEdit::Clear();
  216.   ClearModify();   // reset edit control
  217.   return clear ? TRUE : LoadData();
  218. }
  219.  
  220. #endif
  221. #if !defined(SECTION) || SECTION == 2
  222.  
  223. IMPLEMENT_STREAMABLE2(TMagmaEditView, TMagmaEditSearch, TView);
  224.  
  225. void*
  226. TMagmaEditView::Streamer::Read(ipstream& is, uint32 /*version*/) const
  227. {
  228.   ReadBaseObject((TMagmaEditSearch*)GetObject(), is);
  229.   ReadBaseObject((TView*)GetObject(), is);
  230.   is >> GetObject()->Origin;
  231.   return GetObject();
  232. }
  233.  
  234. void
  235. TMagmaEditView::Streamer::Write(opstream& os) const
  236. {
  237.   WriteBaseObject((TMagmaEditSearch*)GetObject(), os);
  238.   WriteBaseObject((TView*)GetObject(), os);
  239.   os << GetObject()->Origin;
  240. }
  241.  
  242. #endif
  243.  
  244.